package com.mymorningroutine;
import android.view.View.*;
import android.widget.*;
import android.os.*;
import android.view.*;
import com.mymorningroutine.obj.*;
import java.util.*;
import android.widget.AdapterView.*;
import android.content.*;
import android.app.*;

public class RoutineSectionFragment extends RefreshableFragment
{

		/**
		 * The fragment argument representing the section number for this
		 * fragment.*/
		private ArrayList<Routine> routinesList;
		protected static TextView textHelp;
		protected static ListView listView;
		private Button createRoutineButton;


		public RoutineSectionFragment(){
			super();
		}
		
	@Override
	protected void setTextHelp(TextView textView)
	{
		this.textHelp = textView;
	}

	@Override
	protected TextView getTextHelp()
	{
		// TODO: Implement this method
		return textHelp;
	}
		
	@Override
	protected void setListView(ListView inListView)
	{
		// TODO: Implement this method
		listView = inListView;
	}

	@Override
	protected ListView getListView()
	{
		// TODO: Implement this method
		return listView;
	}
		
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
								 Bundle savedInstanceState)
		{
			rootView = inflater.inflate(R.layout.fragment_routines, null);
			createRoutineButton = (Button) rootView.findViewById(R.id.button_add_routine);
			createRoutineButton.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View p1)
					{
						showRoutineTitleDialog(null);


						
					}
					
					}
				);	


			textHelpId = R.id.text_help_routines;
			initiateViews();
			listView.setOnItemClickListener(new OnItemClickListener(){
					

					@Override
					public void onItemClick(AdapterView<?> p1, View view, int p3, long p4)
					{
						
						Integer itemId = view.getId();

						Class<?> itemActivity = RoutineDetailActivity.class;
						Intent intent = new Intent(getActivity(), itemActivity);

						intent.putExtra("routineId", itemId);
						startActivity(intent);


					}
				});
				
			listView.setOnItemLongClickListener(new OnItemLongClickListener(){
					

					@Override
					public boolean onItemLongClick(AdapterView<?> p1, View view, int p3, long p4)
					{
						// TODO: Implement this method
						Routine routine = Singleton.getRoutineTable(getActivity()).getRoutine(view.getId());
						showRoutineTitleDialog(routine);
						return true;
					}


				});
				

			loadRoutines();
			return rootView;
		}




		public void loadRoutines()
		{
			routinesList = Singleton.getRoutineTable(getActivity()).getRoutines();
			listView.setAdapter(new RoutinesAdapter(getActivity(), R.layout.list_routine_item, routinesList));
		}

		@Override
		public void refreshFragment()
		{
			// TODO: Implement this method
			loadRoutines();
		}
		
		public void showRoutineTitleDialog(Routine routinep){
			
			final EditText input = new EditText(getActivity());
			if(routinep != null) {
			input.setText(routinep.getTitle());
			} else {
			input.setText(getResources().getString(R.string.activity_title));
			routinep = new Routine();
			}
			AlertDialog. Builder alertDialogBuilder = new AlertDialog. Builder(getActivity());
// set title
			final Routine routine = routinep;
			alertDialogBuilder. setTitle(getResources().getString(R.string.activity_title)) ;
// set dialog message
						alertDialogBuilder
							. setCancelable(true)
							.setView(input)
							. setPositiveButton(getResources().getString(R.string.save) , new DialogInterface. OnClickListener() {
								public void onClick(DialogInterface dialog, int id)
								{
// if this button is clicked, close
// current activity
									//Delete activity
									String title = input.getText().toString();
									if(title != null && title != ""){
										routine.setTitle(title);
										routine.setActivities(new ArrayList());
										Singleton.getRoutineTable(getActivity()).saveRoutine(routine);
										refreshFragment();
									}
								
								}
							})
							. setNegativeButton(getResources().getString(R.string.cancel) , new DialogInterface. OnClickListener() {
								public void onClick(DialogInterface dialog, int id)
								{
// if this button is clicked, just close
// the dialog box and do nothing
									dialog.cancel() ;
								}
							}) ;
// create alert dialog
						AlertDialog alertDialog = alertDialogBuilder.create();
// show it
						alertDialog.show();
		
		}
		
		
	}
